//
//  LLEmoticonPageViewCell.swift
//  xinlangweibo
//
//  Created by 任玉林 on 16/7/5.
//  Copyright © 2016年 任玉林. All rights reserved.
//

import UIKit

let w = (SCREENW - 10) / CGFloat(EmoticonMaxCol)

let h = (216 - 37 - 20) / CGFloat(EmoticonMaxRow)

class LLEmoticonPageViewCell: UICollectionViewCell {
    
    var emoticons: [LLEmoticonBundleModel]?{
        
        didSet{
            
            //为了防止重复引用cell 所有每次给button赋值都要将其隐藏，需要几个显示几个
            for button in emoticonButtons{
                
                button.hidden = true
        
            }
            
            //遍历一维数组
            for (i, emoticon) in emoticons!.enumerate(){
                
                let button = emoticonButtons[i]
                
                button.hidden = false
                
                button.emoticon = emoticon
                
                if emoticon.isEmoji {
                    
                    let titl = ((emoticon.code ?? "" ) as NSString).emoji()
                    
                    
                    button.setTitle(titl, forState: UIControlState.Normal)
                    
                    button.setImage(nil, forState: UIControlState.Normal)
                    
                }else{
                    
                    let path = emoticon.path ?? ""
                    
                    let png = emoticon.png ?? ""
                    
                    let name = path + png
                    
                    let image = UIImage(named: name, inBundle: LLEmojiTool.sharedTool.emoticonbundle, compatibleWithTraitCollection: nil)

                    button.setImage(image, forState: UIControlState.Normal)
                    
                    button.setTitle(nil , forState: UIControlState.Normal)
                }
                
            }
        }
    }
    
    var indexpath: NSIndexPath?{
        
        didSet{
            
//            messageLabel.text = "第\(indexpath?.section ?? 0)组 第\(indexpath?.item ?? 0)页"
        }
    }
    
    override init(frame: CGRect) {
    super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //存放表情的数组
    var emoticonButtons: [LLEmoticonButton] = [LLEmoticonButton]()
    
    //设置视图
    func setupUI(){
        backgroundColor = UIColor.whiteColor()
        
        addChildeButtons()
        
        contentView.addSubview(deleteButton)
        
//        contentView.addSubview(messageLabel)
        
        
//        messageLabel.snp_makeConstraints { (make) -> Void in
//            
//            make.center.equalTo(contentView)
//        }
        
    }
    
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        for (i , button) in emoticonButtons.enumerate() {
            
            let x = i % EmoticonMaxCol
            
            let y = i / EmoticonMaxCol
            
            button.frame = CGRect(x: CGFloat(x) * w + 5, y: CGFloat(y) * h, width: w, height: h)
            
        }
        
        deleteButton.frame = CGRect(x: SCREENW - w - 5, y: 2 * h , width: w, height: h)
        
        
    }
    
    //表情键盘删除按钮监听事件
    @objc private func deleletButtonClick(){
        
        NSNotificationCenter.defaultCenter().postNotificationName(EmoticonDeleteButtonNoti, object: nil)
        
    }
    
    //表情键盘每一个按钮点击事件
    @objc private func buttonClick(button: LLEmoticonButton){
        
        NSNotificationCenter.defaultCenter().postNotificationName(EmoticonButtonNoti, object: button.emoticon)
    }
    //创建每一页的20个 Button
    
    func addChildeButtons(){
        
        for _ in 0..<EmoticonMaxCount {
            
            let button = LLEmoticonButton()
            
            button.titleLabel?.font = UIFont.systemFontOfSize(32)
//            button.backgroundColor = randomColor()
            
            button.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
            
            emoticonButtons.append(button)
            
            contentView.addSubview(button)
        }
        
    }
    //懒加载
    private lazy var messageLabel: UILabel = {
       
        let lab = UILabel(textColor: UIColor.redColor(), textFont: 35)
        
      lab.textAlignment = .Center
        
        return lab
        
    }()
    
    //表情中的删除按钮
    private lazy var deleteButton: UIButton = {
        
       let button = UIButton()
        
        button.setImage(UIImage(named: "compose_emotion_delete"), forState: UIControlState.Normal)
        
        button.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: UIControlState.Highlighted)
        
        button.addTarget(self, action: "deleletButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
}
